// curriculum_package/pages/purchaseHistory/purchaseHistory.ts
import {getPurchaseHistory} from '../../../api/myCourse'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    selectShow:false,
    dateText:"", //日期时间
    minDate:new Date(1900, 10, 1).getTime(),
    formatter(type:string, value:number) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if(type === 'day'){
        return `${value}日`;
      }
      return value;
    },
    listData:[
    ],
    postData:{
      pageNum:1,
      pageSize:10,
      month:""
    },
    total:0, //总页数
    moreLimt:false,//是否加载完成
    isfreshComplete:false, //是否刷新完成
  },
    // 列表刷新课程
    loadPageEvent(data:any){
      const type = data.detail.type
      switch(type){
        case 'refresh':
          this.resetPage()
          this.getPurchaseRecord()
          this.setData({
            isfreshComplete:true
          })
          break;
         case 'load':
          let formData = this.data.postData as any
          if(formData.pageNum * formData.pageSize < this.data.total){
            formData.pageNum  = formData.pageNum + 1
            this.setData({
              formData
            })
            this.getPurchaseRecord()
          }else{
           this.setData({
            moreLimt: true
           })
          }
          break;
      }
      
    
      },
              // 重置页码
  resetPage(){
    let formData = this.data.postData as any
    formData.pageNum = 1
   this.setData({
    formData,
    listData:[]
   })
  },
    // 获取购买记录接口
    async getPurchaseRecord(){
     const postData = this.data.postData as any
    const {code,data:res} = await getPurchaseHistory(postData)
    if(code === 200){
      const {total,list} = res
      let listData = this.data.listData.concat(list)
      this.setData({
        listData,
        total
      }) 
    }
    },
      //条件筛选选择日期
      selectDate(){
        this.setData({
          selectShow:true
         })   
      },
      // 关闭底部弹框
      onClose(){
       this.setData({
        selectShow:false
       }) 
      },
        // 日期选择改变
        dateSelect(event:any){
          this.setData({
          currentDate: event.detail,
          })
        },
           // 确认日期选择
        dateConfirm(e:any){
           this.dateFormat(e.detail)
            this.setData({
             selectShow:false,
            }) 
            this.resetPage()
          this.getPurchaseRecord()
        },
        // 日期格式化
  dateFormat(timeCash?:number){
    let year = timeCash ? new Date(timeCash).getFullYear() : new Date().getFullYear()
    let month = timeCash ? new Date(timeCash).getMonth() + 1 : new Date().getMonth() + 1
    const postData = this.data.postData
    postData.month = `${year}-${month < 10 ? '0' + month : month}`
    this.setData({
      selectDateText: `${year}年${month}月`,
      postData
    })
    
  },
  // 前往订单详情页
  skipDetail(e:any){
  const {courseSeriesId,stuId,stuName,payment,orderStatus,orderSn,createTime} =  e.currentTarget.dataset.item
  const orderInfo = {
    payment,
    orderStatus,
    orderSn,
    createTime
  }
  wx.navigateTo({
    url:`/myCourse_package/pages/confirmOrders/confirmOrders?stuId=${stuId}&serialId=${courseSeriesId}&userName=${stuName}&orderInfo=${JSON.stringify(orderInfo)}&type=detail`
  })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.dateFormat()
    this.getPurchaseRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})